package com.quectel.core.module.parking.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.car.service.CarService;
import com.quectel.core.module.carparking.service.CarParkingDoorService;
import com.quectel.core.module.device.service.DeviceService;
import com.quectel.core.module.household.service.HouseholdService;
import com.quectel.core.module.parking.dao.ParkingCurrentCarDao;
import com.quectel.core.module.parking.dto.ParkingCurrentCarDto;
import com.quectel.core.module.parking.entity.ParkingCurrentCarEntity;
import com.quectel.core.module.parking.service.ParkingChargingService;
import com.quectel.core.module.parking.service.ParkingCurrentCarService;
import com.quectel.core.module.parking.service.ParkingOrderService;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author ryan
 * @email ryan.ran@quectel.com
 * @date 2023/04/13 11:16
 */
@DubboService
public class ParkingCurrentCarServiceImpl implements ParkingCurrentCarService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ParkingCurrentCarService.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> PARKING_CURRENT_CAR_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "PARKING_CURRENT_CAR_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<ParkingCurrentCarDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(PARKING_CURRENT_CAR_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private ParkingCurrentCarDao parkingCurrentCarDao;
    @Autowired
    private ParkingOrderService parkingOrderService;
    @Autowired
    private ParkingChargingService parkingChargingService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private CarService carService;
    @Autowired
    private HouseholdService householdService;
    @Autowired
    private CarParkingDoorService carParkingDoorService;
    @Autowired
    private VillageService villageService;

    @Override
    public ParkingCurrentCarDto selectOneCar(Long villageId, String license, Byte licenseColor) {
        ParkingCurrentCarEntity parkingCurrentCarEntity = parkingCurrentCarDao.selectOne(new LambdaQueryWrapper<ParkingCurrentCarEntity>()
                .eq(ParkingCurrentCarEntity::getVillageId, villageId)
                .eq(ParkingCurrentCarEntity::getLicense, license)
                .eq(ParkingCurrentCarEntity::getLicenseColor, licenseColor)
                .last(SqlConstants.MYSQL_LIMIT_ONE));
        ParkingCurrentCarDto parkingCurrentCarDto = CopyUtils.copyObj(parkingCurrentCarEntity, ParkingCurrentCarDto.class);
        paddingField(parkingCurrentCarDto);
        return parkingCurrentCarDto;
    }

    @Override
    public ParkingCurrentCarDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                PARKING_CURRENT_CAR_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public ParkingCurrentCarDto selectById(Long id) {
        ParkingCurrentCarEntity entity = parkingCurrentCarDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, ParkingCurrentCarDto.class));
    }

    @Override
    public List<ParkingCurrentCarDto> queryList(Map<String, Object> params) {
        List<ParkingCurrentCarEntity> list = parkingCurrentCarDao.queryList(params);
        List<ParkingCurrentCarDto> result = CopyUtils.copyList(list, ParkingCurrentCarDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属园区等
     *
     * @param dto
     * @return
     */
    private ParkingCurrentCarDto paddingField(ParkingCurrentCarDto dto) {
        if (dto != null) {
            dto.setVillage(villageService.selectCacheById(dto.getVillageId()));
            if (dto.getVillage() != null) {
                dto.setParkName(dto.getVillage().getName() + "停车场");
            }
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return parkingCurrentCarDao.queryTotal(params);
    }

    @Override
    public Long save(ParkingCurrentCarDto dto) {
        ParkingCurrentCarEntity entity = CopyUtils.copyObj(dto, ParkingCurrentCarEntity.class);
        entity.setId(Snowflake.nextId());
        parkingCurrentCarDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(ParkingCurrentCarDto dto) {
        ParkingCurrentCarEntity entity = CopyUtils.copyObj(dto, ParkingCurrentCarEntity.class);

        parkingCurrentCarDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(ParkingCurrentCarDto dto) {
        ParkingCurrentCarEntity entity = CopyUtils.copyObj(dto, ParkingCurrentCarEntity.class);

        MyBatisPlusUtils.updateAllColumnById(entity, parkingCurrentCarDao);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id) {

        CLEAR_CACHE_FUNC.accept(selectById(id));

        parkingCurrentCarDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
